package com.ln.asset.core.controller;


import com.ln.asset.common.result.R;
import com.ln.asset.core.pojo.Dict;
import com.ln.asset.core.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cuixiaoxin
 * @since 2022-04-29
 */

@Api(tags = "数据字典")
@RestController
@RequestMapping("/admin/core/dict")
@CrossOrigin
@Slf4j
public class DictController {

    @Resource
    private DictService dictService;

    @ApiOperation("数据字典")
    @GetMapping("/list")
    public R ShowDict() {
        List<Dict> dictList = dictService.queryDictList();
        return R.ok().data("dictList", dictList);
    }

    @ApiOperation("根据pid查询出所有的内容")
    @PostMapping("/show/{id}")
    public R ShowById(
            @ApiParam(value = "当前的id", required = true)
            @PathVariable Integer id) {
        if(id==4){
            List<Dict> dictList = dictService.selectDictLingJianByPid(id);
            return R.ok().data("dictList", dictList);
        }else{
            List<Dict> dictList = dictService.selectDictByPid(id);
            return R.ok().data("dictList", dictList);
        }
    }

//    @ApiOperation("根据pid和value查询信息")
//    @PostMapping("/showByPid/{pid}/{value}")
//    public R ShowByPId(
//            @ApiParam(value = "当前的pid", required = true)
//            @PathVariable Integer pid,
//            @ApiParam(value = "当前的value", required = true)
//            @PathVariable String value
//    ) {
//        List<Dict> dictList = dictService.selectDictById(pid,value);
//        return R.ok().data("dictList", dictList);
//    }

    @ApiOperation("根据dict_code和value查询信息")
    @PostMapping("showDictCode/{dictcode}/{value}")
    public R ShowByDict(
            @ApiParam(value = "当前的编码", required = true)
            @PathVariable String dictcode,
            @ApiParam(value = "当前的value", required = true)
            @PathVariable String value
    ) {
        if(dictcode.equals("part")){
            Dict dictList = dictService.selectDictLingJianByDict(dictcode,value);
            return R.ok().data("dictList", dictList);
        }else{
            Dict dictList = dictService.selectDictByDict(dictcode,value);
            return R.ok().data("dictList", dictList);
        }
    }

    /**
     * luguangyun
     * @param dictCode
     * @return
     */
    @ApiOperation("根据dictCode获取下级节点")
    @RequestMapping("/assetCat/{dictCode}")
    public R assetCat(
            @ApiParam(value = "节点编码" ,required = true)
            @PathVariable String dictCode){
        List<Dict> list = dictService.findByDictCode(dictCode);
        return R.ok().data("list",list);
    }
}

